package com.liuzhilong.infrastructure.framework.algorithms.datastruct.graph;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * <h1>图-顶点定义</h1>
 *
 * 主要元素：key和边的列表集合
 *
 */
@Data
@Accessors(chain = true)
public class Vertex<T> {
    //自带的key信息 需要实现hashcode和equals方法
    private T key;

    private Set<Edge<T>> next = new HashSet<>();

    public Vertex<T> addEdge(Edge edge){
        next.add(edge);
        return this;
    }


    public static final <T> Vertex<T> create(T key){
        return new Vertex<T>().setKey(key);
    }


    @Override
    public String toString() {
        return "Vertex{" +
                "key=" + key +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o){ return true;}
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Vertex<?> vertex = (Vertex<?>) o;
        return key.equals(vertex.key);
    }

    @Override
    public int hashCode() {
        return Objects.hash(key);
    }
}
